package com.app.controller;

import com.app.utils.MD5;
import com.app.utils.Result;
import com.core.aspect.Log;
import com.news.entity.Content;
import com.news.service.ContentService;
import com.sys.user.entity.SysUser;
import com.utils.TmStringUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录控制器
 * 
 * @author Gaojun.Zhou
 * @date 2016年12月14日 下午2:54:01
 */
@Controller
@RequestMapping("")
public class LoginController {
	
	@Autowired
	ContentService contentService;
	
	/**
	 * 登录页面
	 */
	@Log("进入登录")
	@RequestMapping(value = "/login.do")
	public String login(Model model) {
		return "login";
	}

	/**
	 * 执行登录
	 */
	@RequestMapping(value = "/login/doLogin.do", method = RequestMethod.POST)
	@ResponseBody
	public Result doLogin(ServletRequest request, ServletResponse response,String userName, String password) {
		if(StringUtils.isBlank(userName)) {
			return Result.returnError("账户不能为空");
		}
		if(StringUtils.isBlank(password)) {
			return Result.returnError("密码不能为空");
		}
		password= MD5.toMD5(password);
		Subject currentUser = SecurityUtils.getSubject();
		// 之前登陆的用户
		SysUser user= (SysUser) currentUser.getPrincipal();
		// 如果存在用户，则先退出之前登陆的用户
		if (user != null) {
			currentUser.logout();
			currentUser = SecurityUtils.getSubject();
		}
		UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
		if (!currentUser.isAuthenticated()) {
			// token.setRememberMe(true);
			try {
				currentUser.login(token);
			} catch (UnknownAccountException uae) {
				return Result.returnError("账户不存在");
			} catch (IncorrectCredentialsException ice) {
				return Result.returnError("密码错误");
			} catch (LockedAccountException lae) {
				return Result.returnError("账号已锁定");
			} catch (AuthenticationException ae) {
				// unexpected condition? error?
				return Result.returnError("服务器繁忙");
			}
		}
		return Result.returnSuccess();
	}
	
	@Log("用户退出")
	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)  
	public String logout(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {  
		Subject currentUser = SecurityUtils.getSubject();  
	    if(currentUser != null){  
	    	currentUser.logout();  
	    }  
	    return "redirect:/login.do";  
	}  

	/**
	 * 提供静态化模板跳转
	 * @return
	 */
	@Log("调用静态化模板")
	@RequestMapping(value = "/content")
	public String content() {
		return "/staticHtml/content";
	}
	
	@Log("查询静态化模板")
	@RequestMapping(value = "/openHtml")
	public String openHtml(Integer id) {
		Content content=contentService.selectById(id);
		if(content!=null && TmStringUtils.isNotEmpty(content.getStaticLink())) {
			return content.getStaticLink().replaceAll(".html", "");
		}else {
			return "fail";
		}
	}
	
	
	
}
